import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import org.jfree.chart.JFreeChart;


@SuppressWarnings("serial")
public class Interface extends JFrame implements ActionListener
{
	/* 
	 * Class constructing the interface
	 */
	
	private JPanel panel;
    private JPanel panel_west;
    private JPanel panel_east;
    private JPanel panel_north;
    private JPanel panel_south;
    
    public static JTextArea textZone;
    private static JScrollPane scroll_text;

    private static Download_web thread1 = null;
    private static Download_web thread2 = null;
    private static Download_web thread3 = null;
    
    private static Download_csv thread1_file = null;
    private static Download_csv thread2_file = null;
    private static Download_csv thread3_file = null;
    
    private static JMenuItem menuItem1;
    private static JMenuItem menuItem2;
    
    public static JLabel pourcent1 = new JLabel ("0%");
    public static JLabel pourcent2 = new JLabel ("0%");
    public static JLabel pourcent3 = new JLabel ("0%");
    
    private JLabel eukarya = new JLabel("Eukarya");
    private JLabel archaea = new JLabel("Archaea");
    private JLabel bacteria = new JLabel("Bacteria");
    
    public static JProgressBar progressBar1 = null;
    public static JProgressBar progressBar2 = null;
    public static JProgressBar progressBar3 = null;
    
    public static JFreeChart chart1;
    public static JFreeChart chart2;
    public static JFreeChart chart3;
    
    public static Stats stats_eukarya  = null;
    public static Stats stats_bacteria = null;
    public static Stats stats_archaea  = null;
    
    public static JPanel panel_south1 = new JPanel();
    public static JPanel panel_south2 = new JPanel();
    public static JPanel panel_south3 = new JPanel();
	
	public Interface() throws InterruptedException
	{        
        this.setTitle("Arn statistics");
        this.setSize(1000, 650);
        //this.setResizable(false);
        this.setIconImage(new ImageIcon("arn.png").getImage());
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
        panel = new JPanel();
        panel_north = new JPanel();
        panel_south = new JPanel();
        panel_west = new JPanel();
        panel_east = new JPanel();
        
        panel.setLayout(new BorderLayout()); 
        panel.setLayout(new GridLayout(2, 1));
        panel.add(panel_north);        
        panel.add(panel_south);
        
        panel_north.setLayout(new GridLayout(1, 2));
        panel_north.add(panel_west);
        panel_north.add(panel_east);
        panel_west.setBackground(Color.decode("#CDEBEB"));
        panel_west.setLayout(new GridLayout(7, 3));
        
        textZone = new JTextArea();
        textZone.setEditable(false);
        textZone.setFont(new Font("Calibri", Font.PLAIN, 12));
        scroll_text = new JScrollPane(textZone);

        // Auto-scroll
        scroll_text.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener()  
	        {  
		        public void adjustmentValueChanged(AdjustmentEvent e)  
		        {  
		        	e.getAdjustable().setValue(e.getAdjustable().getMaximum());
		        }
	        });  
        
        panel_east.setLayout(new BorderLayout()); 
        panel_east.add(scroll_text);
        
        JMenuBar menuBar;
        JMenu menu1, menu2;

        //Create the menu bar.
        menuBar = new JMenuBar();

        //Build the first menu.
        menu1 = new JMenu("File");
        menu2 = new JMenu("About");
        menuBar.add(menu1);
        menuBar.add(menu2);

        //a group of JMenuItems
        menuItem1 = new JMenuItem("Quit");
        menuItem1.addActionListener(this);
        menuItem2 = new JMenuItem("Help");
        menuItem2.addActionListener(this);
 
        menu1.add(menuItem1);
        menu2.add(menuItem2);
        
        progressBar1 = new JProgressBar(0, 100); 
        progressBar2 = new JProgressBar(0, 100); 
        progressBar3 = new JProgressBar(0, 100); 

        JLabel download = new JLabel("<html><u>Downloads</u></html>");
        download.setHorizontalAlignment(SwingConstants.CENTER);
        
        panel_west.add("left", new JLabel());
        panel_west.add("Center", download);
        panel_west.add("right", new JLabel());

        pourcent1.setHorizontalAlignment(SwingConstants.CENTER);
        pourcent2.setHorizontalAlignment(SwingConstants.CENTER);
        pourcent3.setHorizontalAlignment(SwingConstants.CENTER);
        
        eukarya.setHorizontalAlignment(SwingConstants.CENTER);
        archaea.setHorizontalAlignment(SwingConstants.CENTER);
        bacteria.setHorizontalAlignment(SwingConstants.CENTER);
        
        panel_west.add("left", eukarya);
        panel_west.add("Center", progressBar1);
        panel_west.add("right", pourcent1);
        
        panel_west.add("left", new JLabel());
        panel_west.add("Center", new JLabel());
        panel_west.add("right", new JLabel());
        
        panel_west.add("left", archaea);
        panel_west.add("Center", progressBar2);
        panel_west.add("right", pourcent2);
        
        panel_west.add("left", new JLabel());
        panel_west.add("Center", new JLabel());
        panel_west.add("right", new JLabel());
        
        panel_west.add("left", bacteria);
        panel_west.add("Center", progressBar3);
        panel_west.add("right", pourcent3);
        
        progressBar1.setValue(0);
        progressBar2.setValue(0);
        progressBar3.setValue(0);
        
        JLabel test = new JLabel("Statistics in progress...");
        panel_south.add("center", test);
        
        this.setJMenuBar(menuBar);    
        this.setContentPane(panel);
        this.setVisible(true);
       
        // user interface
        Popup p = new Popup(null, "tRNA sources", true);
        p.setVisible(true);
        
        // Wait that the user popup is closed
        while (p.isActive())
        {}
        
        // Display the stats
        panel_south.remove(test);
     
        panel_south.setLayout(new GridLayout(1, 3)); 
        
        panel_south.add(panel_south1);
        panel_south.add(panel_south2);
        panel_south.add(panel_south3);
	}

	public void actionPerformed(ActionEvent e) 
	{
		if (e.getSource() == menuItem1)
		{
			this.dispose();
		}
		else if (e.getSource() == menuItem2)
		{
			// Display authors
			JOptionPane.showMessageDialog(null,
				"This software has been developped by S.Wilt and L. Specht.\nCopyright 2012",
				"About",
				JOptionPane.INFORMATION_MESSAGE,
				null);
		}
	}
	
	public static class Popup extends JDialog implements ActionListener 
	{
		private JButton ok;
		private JButton cancel;
		
		private JRadioButton button_eukarya_download;
		private JRadioButton button_eukarya_fromfile;
		private JRadioButton button_archaea_download;
		private JRadioButton button_archaea_fromfile;
		private JRadioButton button_bacteria_download;
		private JRadioButton button_bacteria_fromfile;
		
		private JButton button_eukarya_choose;
		private JButton button_archaea_choose;
		private JButton button_bacteria_choose;
		
		public Popup(JFrame parent, String title, boolean modal) 
		{
			super(parent, title, modal);
			this.setSize(400, 200);
			this.setLocationRelativeTo(null);
			this.setResizable(false);
			this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
			
			this.getContentPane().setLayout(new BorderLayout());
			JPanel download = new JPanel();
			JPanel controls = new JPanel();
			
			download.setLayout(new GridLayout(3,1));

			// Eukarya
			JPanel panel_eukarya = new JPanel();			
			panel_eukarya.setLayout(new GridLayout(1,3));
			
			panel_eukarya.setBorder(BorderFactory.createTitledBorder(" Eukarya "));
			
			button_eukarya_download = new JRadioButton("Download");
			button_eukarya_fromfile = new JRadioButton("Use file :");
			button_eukarya_choose   = new JButton("Eukarya.csv");
			button_eukarya_fromfile.setSelected(true);
			
			ButtonGroup eukarya_group = new ButtonGroup();
			eukarya_group.add(button_eukarya_download);
			eukarya_group.add(button_eukarya_fromfile);
			
			panel_eukarya.add(button_eukarya_download);
			panel_eukarya.add(button_eukarya_fromfile);
			panel_eukarya.add(button_eukarya_choose);
			download.add(panel_eukarya);
			
			// Archaea
			JPanel panel_archaea = new JPanel();
			panel_archaea.setLayout(new GridLayout(1,2));
			
			panel_archaea.setBorder(BorderFactory.createTitledBorder(" Archaea "));
			
			button_archaea_download = new JRadioButton("Download");
			button_archaea_fromfile = new JRadioButton("Use file :");
			button_archaea_choose   = new JButton("Archaea.csv");
			button_archaea_fromfile.setSelected(true);
			
			ButtonGroup archaea_group = new ButtonGroup();
			archaea_group.add(button_archaea_download);
			archaea_group.add(button_archaea_fromfile);
			
			panel_archaea.add(button_archaea_download);
			panel_archaea.add(button_archaea_fromfile);
			panel_archaea.add(button_archaea_choose);
			download.add(panel_archaea);
			
			// Bacteria
			JPanel panel_bacteria = new JPanel();	
			panel_bacteria.setLayout(new GridLayout(1,2));
			
			panel_bacteria.setBorder(BorderFactory.createTitledBorder(" Bacteria "));
			
			button_bacteria_download = new JRadioButton("Download");
			button_bacteria_fromfile = new JRadioButton("Use file :");
			button_bacteria_choose   = new JButton("Bacteria.csv");
			button_bacteria_fromfile.setSelected(true);
			
			ButtonGroup bacteria_group = new ButtonGroup();
			bacteria_group.add(button_bacteria_download);
			bacteria_group.add(button_bacteria_fromfile);
			
			panel_bacteria.add(button_bacteria_download);
			panel_bacteria.add(button_bacteria_fromfile);
			panel_bacteria.add(button_bacteria_choose);
			download.add(panel_bacteria);
			
			ok     = new JButton("Ok");
			cancel = new JButton("Cancel");
			
			controls.add(ok);
			controls.add(cancel);
			
			ok.addActionListener(this);
			cancel.addActionListener(this);
			
			button_eukarya_choose.addActionListener(this);
			button_archaea_choose.addActionListener(this);
			button_bacteria_choose.addActionListener(this);
			
			download.setBackground(Color.white);
			
			this.getContentPane().add(download, BorderLayout.CENTER);
			this.getContentPane().add(controls, BorderLayout.SOUTH);
		}
		
		@Override
		public void actionPerformed(ActionEvent e) 
		{			
			String eukarya_file = "Eukarya.csv";
			String archaea_file = "Archaea.csv";
			String bacteria_file = "Bacteria.csv";
			
			Database db_eukarya  = new Database("Eukarya");
			Database db_archaea  = new Database("Archaea");
			Database db_bacteria = new Database("Bacteria");
			
			try 
			{
				if (e.getSource() == button_eukarya_choose)
				{
					JFileChooser chooser = new JFileChooser();
					int returnVal = chooser.showOpenDialog(getParent());
				    if(returnVal == JFileChooser.APPROVE_OPTION)
				    	eukarya_file = chooser.getSelectedFile().getAbsolutePath();
				    
				    button_eukarya_choose.setText(eukarya_file);
				}
				else
				if (e.getSource() == button_archaea_choose)
				{
					JFileChooser chooser = new JFileChooser();
					int returnVal = chooser.showOpenDialog(getParent());
				    if(returnVal == JFileChooser.APPROVE_OPTION)
				    	archaea_file = chooser.getSelectedFile().getAbsolutePath();
				   
				    button_archaea_choose.setText(archaea_file);
				}
				else
				if (e.getSource() == button_bacteria_choose)
				{
					JFileChooser chooser = new JFileChooser();
					int returnVal = chooser.showOpenDialog(getParent());
				    if(returnVal == JFileChooser.APPROVE_OPTION)
				    	bacteria_file = chooser.getSelectedFile().getAbsolutePath();
				    
				    button_bacteria_choose.setText(bacteria_file);
				}
				else
				if (e.getSource() == ok)
				{
					this.setVisible(false);
					// Download Eukarya or use the file?
					if(button_eukarya_download.isSelected())
					{
						thread1 = new Download_web(db_eukarya);
					    thread1.start();
					} 
					else
					{
						// Creating database from csv
						thread1_file = new Download_csv(db_eukarya, eukarya_file, "Eukarya_stats.csv");
						thread1_file.start();
					}
					
					// Download Archaea or use the file?
					if(button_archaea_download.isSelected())
					{
						thread2 = new Download_web(db_archaea);
					    thread2.start();
					}
					else
					{
						// Creating database from csv
						thread2_file = new Download_csv(db_archaea, archaea_file, "Archaea_stats.csv");
						thread2_file.start();
					}
					
					// Download Bacteria or use the file?
					if(button_bacteria_download.isSelected())
					{
						thread3 = new Download_web(db_bacteria);
					    thread3.start();
					}
					else
					{
						// Creating database from csv
						thread3_file = new Download_csv(db_bacteria, bacteria_file, "Bacteria_stats.csv");
						thread3_file.start();		
					}
				}			
				else if (e.getSource() == cancel)
				{
					// Close the window
					System.exit(1);
				}
			}
			catch (Exception e1)
			{e1.printStackTrace();}
		}
	}
}


